/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.datasets.service.impl;

import com.medusa.gruul.account.api.feign.RemoteMiniAccountService;
import com.medusa.gruul.account.api.model.AccountInfoDto;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.CurUserUtil;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import com.medusa.gruul.common.dto.CurUserDto;
import com.medusa.gruul.datasets.api.entity.DcShopActiveAnalyse;
import com.medusa.gruul.datasets.api.model.CallerDto;
import com.medusa.gruul.datasets.mapper.DcShopActiveAnalyseMapper;
import com.medusa.gruul.datasets.service.IDcShopActiveAnalyseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.datasets.web.model.CallerInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 访客数据统计表 服务实现类
 * </p>
 *
 * @author xiaoq
 * @since 2020-05-06
 */
@Service
public class DcShopActiveAnalyseServiceImpl extends ServiceImpl<DcShopActiveAnalyseMapper, DcShopActiveAnalyse>
		implements IDcShopActiveAnalyseService {

	@Autowired
	private DcShopActiveAnalyseMapper dcShopActiveAnalyseMapper;


	/**
	 * 分装方法 年月
	 *
	 * @return
	 */
	private Integer communalYyyyMM() {
		Integer yyyyMM = Integer.valueOf(String.valueOf(new SimpleDateFormat("yyyyMM").format(new Date())));
		return yyyyMM;
	}

	/**
	 * 分装方法 年月日
	 *
	 * @return
	 */
	private Integer communalYyyyMMdd() {
		Integer yyyyMMdd = Integer.valueOf(String.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date())));
		return yyyyMMdd;
	}


	/**
	 * 新增访客数据
	 *
	 */
	@Override
	public void addCallerInfo() {
		CurUserDto feignCurUser = CurUserUtil.getHttpCurUser();
		if (feignCurUser == null) {
			throw new ServiceException("数据错误");
		}
		DcShopActiveAnalyse callerInfo = dcShopActiveAnalyseMapper.getCallerInfoByToday();
		if (callerInfo == null) {
			callerInfo = new DcShopActiveAnalyse();
			callerInfo.setPv(1);
			callerInfo.setUv(1);
			callerInfo.setYyyyMM(communalYyyyMM());
			callerInfo.setYyyyMMdd(communalYyyyMMdd());
			int insert = dcShopActiveAnalyseMapper.insert(callerInfo);
			if (insert < 1) {
				throw new ServiceException("新增访客数据失败", SystemCode.DATA_ADD_FAILED.getCode());
			}
		} else {
			callerInfo.setPv(callerInfo.getPv() + 1);
			callerInfo.setYyyyMM(communalYyyyMM());
			callerInfo.setYyyyMMdd(communalYyyyMMdd());
			int update = dcShopActiveAnalyseMapper.updateById(callerInfo);
			if (update < 1) {
				throw new ServiceException("修改访客数据失败", SystemCode.DATA_UPDATE_FAILED.getCode());
			}
		}
	}


	/**
	 * 新增访客数据
	 * @param shopId  商铺id
	 * @param tenantId 租户id
	 */
	@Override
	public void saveCallerInfo(String shopId, String tenantId) {
		DcShopActiveAnalyse callerInfo = dcShopActiveAnalyseMapper.getCallerInfoByShopId(shopId, tenantId);
		if (callerInfo == null) {
			callerInfo = new DcShopActiveAnalyse();
			callerInfo.setPv(1);
			callerInfo.setUv(1);
			callerInfo.setShopId(shopId);
			callerInfo.setTenantId(tenantId);
			callerInfo.setYyyyMM(communalYyyyMM());
			callerInfo.setYyyyMMdd(communalYyyyMMdd());
			int insert = dcShopActiveAnalyseMapper.insert(callerInfo);
			if (insert < 1) {
				throw new ServiceException("新增访客数据失败", SystemCode.DATA_ADD_FAILED.getCode());
			}
		} else {
			callerInfo.setPv(callerInfo.getPv() + 1);
			callerInfo.setUv(callerInfo.getUv() + 1);
			callerInfo.setYyyyMM(communalYyyyMM());
			callerInfo.setYyyyMMdd(communalYyyyMMdd());
			int update = dcShopActiveAnalyseMapper.updateById(callerInfo);
			if (update < 1) {
				throw new ServiceException("修改访客数据失败", SystemCode.DATA_UPDATE_FAILED.getCode());
			}
		}
	}

	/**
	 * 根据时间来筛选浏览量 当结束时间为null是 提供当日浏览量及昨日浏览量
	 *
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return
	 */
	@Override
	public CallerDto getCallerInfo(Integer startTime, Integer endTime) {
		CallerDto callerDto = new CallerDto();
		if (endTime == null) {
			//获取今日浏览量
			DcShopActiveAnalyse callerInfoToday = dcShopActiveAnalyseMapper.getCallerInfoByToday();
			//获取昨日浏览量
			DcShopActiveAnalyse callerInfoYesterday = dcShopActiveAnalyseMapper.getCallerInfoByYesterday();
			//昨日
			callerDto.setYesterdayPv(0);
			callerDto.setPv(0);
			if (callerInfoYesterday != null) {
				callerDto.setYesterdayPv(callerInfoYesterday.getPv());
			}
			//今日
			if (callerInfoToday != null) {
				callerDto.setPv(callerInfoToday.getPv());
			}

		} else {
			//查询开始时间到结束时间的 浏览量和 昨日浏览量值为-1
			Integer pv = dcShopActiveAnalyseMapper.getCallerInfoByTime(startTime, endTime);
			pv = pv == null ? 0 : pv;
			callerDto.setPv(pv);
			callerDto.setYesterdayPv(-1);
		}
		return callerDto;
	}

	/**
	 * 访客数据信息获取
	 *
	 * @return 访客数据信息
	 */
	@Override
	public CallerInfoVo getShopCallerInfo() {
		CallerInfoVo callerInfoVo = new CallerInfoVo();
		//当日浏览量 访客数
		DcShopActiveAnalyse callerInfo = dcShopActiveAnalyseMapper.getCallerInfoByToday();
		//总访客数
		Integer totalUv = dcShopActiveAnalyseMapper.getAllCallerInfoByShopId();
		//总浏览量
		if (callerInfo == null) {
			callerInfoVo.setPv(0);
			callerInfoVo.setUv(0);
		} else {
			Integer pv = callerInfo.getPv() == null ? 0 : callerInfo.getPv();
			Integer uv = callerInfo.getUv() == null ? 0 : callerInfo.getUv();
			callerInfoVo.setUv(uv);
			callerInfoVo.setPv(pv);
		}
		totalUv = totalUv == null ? 0 : totalUv;
		callerInfoVo.setTotalUv(totalUv);
		return callerInfoVo;
	}
}
